//
//  MainViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/20.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "MainViewController.h"
#import "MainCell.h"
#import "MainOrderModel.h"
#import "ScanModel.h"
#import "RankModel.h"
#import "BannerModel.h"

#import "RecordViewController.h"
#import "AddViewController.h"
#import "ContactViewController.h"

#import "ScanViewController.h"
#import "RankViewController.h"

#import "MyOrderViewController.h"

#import <CoreLocation/CoreLocation.h>

#import "ChatViewController.h"
#import "ChatListViewController.h"
#import "ContactsViewController.h"
#import "InfoViewController.h"
#import "SearchViewController.h"
#import "CardDetailViewController.h"

@interface MainViewController () <UIScrollViewDelegate,XLCycleScrollViewDatasource,XLCycleScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate,UIActionSheetDelegate,UIGestureRecognizerDelegate>

@end

@implementation MainViewController
{
    
    UIScrollView *_scrollView;
    XLCycleScrollView *_photoView;
    UIView *_fastEntryView;
    UIView *_hotView;
    UIView *_hotDetailView;
    UIView *_subscribeView;
    UITableView *_tableView;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;
    
    UIImageView *_headImage;
    UILabel *_nameLabel;
    UILabel *_gradeLabel;
    UILabel *_schoolLabel;
    UILabel *_signatureLabel;
    
    
    UILabel *_scanNum;
    UILabel *_rankNum;
    
    CLLocationManager *_locationManager;
    NSString *_lat;
    NSString *_lng;
    NSMutableArray *_dataArray;
    MainOrderModel *_mainOrderModel;
    ScanModel *_scanModel;
    RankModel *_rankModel;
    NSMutableArray *_photoArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil];
    [self creatNavigationBarRightItemWithRightTitle:nil RightImage:ECIMAGENAME(@"main_navc_r.png")];
    [self creatNavigationBarLeftItemWithLeftTitle:@"上海" LeftImage:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshNumLabel:) name:@"refreshNumLabel" object:nil];

    [self creatLocationManager];
    [self creatScrollView];
    [self creatPhotoView];
    [self creatFastEntry];
    [self creatHotView];
    [self creatHotDetailView];
    [self creatTableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(65*3);
    }];
}


- (void)refreshNumLabel:(NSNotification *)noti
{
    NSString *num = [noti object];
    if ([num integerValue] > 0) {
        self.numLabel.hidden = NO;
        self.numLabel.text = num;
    }else{
        self.numLabel.hidden = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [_locationManager startUpdatingLocation];
    [self loadUserInfo];
    [self reloadHotDetailWithMemberModel:_memberModel TeacherModel:_teacherModel];
    [self creatFirstHttpRequest];
    [self creatSecondHttpRequest];
    [self creatThirdHttpRequest];
    [self creatFourthHttpRequest];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_locationManager stopUpdatingLocation];
}


- (void)searchBtnClick:(id)sender
{
    NSLog(@"*****搜索*****");
    SearchViewController *vc= [[SearchViewController alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.navigationController presentViewController:navc animated:YES completion:nil];
}

- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
    _lat = userInfo.lat;
    _lng = userInfo.lng;
}


- (void)creatLocationManager
{
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [_locationManager requestWhenInUseAuthorization];
    }
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        [_locationManager requestWhenInUseAuthorization];
    }
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
        _locationManager.allowsBackgroundLocationUpdates = NO;
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    for (CLLocation *userLocation in locations) {
        UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
        [userInfo updateLocationWithLat:[NSString stringWithFormat:@"%f",userLocation.coordinate.latitude] WithLng:[NSString stringWithFormat:@"%f",userLocation.coordinate.longitude]];
        NSLog(@"***CLLOCATION***%@***",userLocation);
    }
}


- (void)creatFirstHttpRequest
{
    NSDictionary *param = @{
                            @"t_member_Id":_memberModel.member_Id,
                            @"promulgator_mark":@"1",
                            @"pageIndex":@"0",
                            @"pageSize":@"999",
                            };
    
    [[HttpManager defaultManager]postRequestToUrl:RequestURL(ORDER_P) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [MainOrderModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"MainOrderData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            _mainOrderModel = [[MainOrderModel alloc] init];
            _mainOrderModel = [MainOrderModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_mainOrderModel.data];
            _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 490+65*_dataArray.count);
            [_subscribeView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_greaterThanOrEqualTo(_dataArray.count*65 + 40);
            }];
            [self.view layoutIfNeeded];
            [_tableView reloadData];
        }
    }];
}


- (void)creatSecondHttpRequest
{
    NSDictionary *param = @{
                            @"teacher_Id":_memberModel.member_Id,
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(SCAN) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [ScanModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"ScanData",
                         };
            }];
            
            _scanModel = [[ScanModel alloc] init];
            _scanModel = [ScanModel objectWithKeyValues:result];
            NSInteger scanNum = 0;
            for (ScanData *data in _scanModel.data) {
                scanNum += [data.total integerValue];
            }
            if (scanNum < 100) {
                _scanNum.text = [NSString stringWithFormat:@"%ld",scanNum];
            }else{
                _scanNum.text = @"99+";
            }
        }
    }];
}


- (void)creatThirdHttpRequest
{
    NSDictionary *param = @{
                            @"member_Id":_memberModel.member_Id,
                            @"pageIndex":@"0",
                            @"pageSize":@"100",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(RANK) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [RankModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"RankData",
                         };
            }];
            
            _rankModel = [[RankModel alloc] init];
            _rankModel = [RankModel objectWithKeyValues:result];
            
            if (_rankModel.data.count < 100) {
                _rankNum.text = [NSString stringWithFormat:@"%ld",_rankModel.data.count];
            }else{
                _rankNum.text = @"99+";
            }
        }
    }];
}


- (void)creatFourthHttpRequest
{
    NSDictionary *param = @{
                            @"MARKING":@"1",
                            @"pageIndex":@"0",
                            @"pageSize":@"999",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(BANNER) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [BannerModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"BannerData",
                         };
            }];
            
            if (_photoArray) {
                [_photoArray removeAllObjects];
            }else{
                _photoArray = [[NSMutableArray alloc] init];
            }
            
            BannerModel *model = [[BannerModel alloc] init];
            model = [BannerModel objectWithKeyValues:result];
            [_photoArray addObjectsFromArray:model.data];
            [_photoView reloadData];
        }
    }];
}

- (void)reloadHotDetailWithMemberModel:(MemberModel *)member TeacherModel:(TeacherModel *)teacher
{
    if (member.name.length > 0) {
        _nameLabel.text = member.name;
    }else{
        _nameLabel.text = @"暂无数据";
    }
    
    if (member.school.length > 0) {
        _schoolLabel.text = [NSString stringWithFormat:@"毕业院校:%@",member.school];
    }else{
        _schoolLabel.text = @"毕业院校:暂无数据";
    }
    
    if (member.signature.length > 0) {
        _signatureLabel.text = [NSString stringWithFormat:@"签名:%@",member.signature];
    }else{
        _signatureLabel.text = @"暂无数据";
    }
    
    if (teacher.specialty.length > 0) {
        _gradeLabel.text = [NSString stringWithFormat:@"专业:%@",teacher.specialty];
    }else{
        _gradeLabel.text = @"暂无数据";
    }
    
    [_headImage sd_setImageWithURL:[NSURL URLWithString:member.imgUrl] placeholderImage:ECIMAGENAME(@"header.png")];
}

- (void)creatScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64 - 49)];
    _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 490);
    _scrollView.backgroundColor = BG_COLOR;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchScrollView)];
    [recognizer setNumberOfTapsRequired:1];
    [recognizer setNumberOfTouchesRequired:1];
    recognizer.delegate = self;
    [_scrollView addGestureRecognizer:recognizer];
}


- (void)touchScrollView
{
    [self.searchTextField resignFirstResponder];
}

- (void)creatPhotoView
{
    _photoView = [[XLCycleScrollView alloc]initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 150) withOutTimer:YES];
    _photoView.delegate = self;
    _photoView.datasource = self;
    _photoView.pageControl.hidden = NO;
    [_scrollView addSubview:_photoView];
}


- (NSInteger)numberOfPages
{
    return _photoArray.count;
}



- (UIView *)pageAtIndex:(NSInteger)index
{
    BannerData *data = _photoArray[index];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_photoView.frame];
    imageView.backgroundColor = [UIColor whiteColor];
    [imageView sd_setImageWithURL:[NSURL URLWithString:data.banner_IMGURL] placeholderImage:ECIMAGENAME(@"placeImage.png")];
    return imageView;
}


- (void)creatFastEntry
{
    _fastEntryView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, ECSCREEN_W, 80)];
    [_scrollView addSubview:_fastEntryView];
    
    
    NSArray *imageArr = @[@"main_fast_1.png",@"main_fast_2.png",@"main_fast_3.png",@"main_fast_4.png"];
    NSArray *titleArr = @[@"我要看记录",@"我要上课",@"我要解答",@"我要聊天"];
    
    for (int i = 0; i < imageArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100 + i;
        button.frame = CGRectMake(ECSCREEN_W/4 *i + (ECSCREEN_W/4 - 53)/2, 10, 53, 53);
        [button setBackgroundImage:ECIMAGENAME(imageArr[i]) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(fastEntryClick:) forControlEvents:UIControlEventTouchUpInside];
        [_fastEntryView addSubview:button];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/4 *i + (ECSCREEN_W/4 - 60)/2, 68, 60, 12)];
        label.text = titleArr[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12.f];
        [_fastEntryView addSubview:label];
    }
    
    
    
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(ECSCREEN_W/4*3 + (ECSCREEN_W/3)/2, 5, 16, 16)];
    self.numLabel.clipsToBounds = YES;
    self.numLabel.layer.cornerRadius = 8;
    self.numLabel.font = [UIFont systemFontOfSize:10.f];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.backgroundColor = NAVC_COLOR;
    self.numLabel.textColor = ECCOLOR(255, 255, 255, 1);
    [_fastEntryView addSubview:self.numLabel];

    if (self.tabBarItem.badgeValue) {
        self.numLabel.hidden = NO;
        self.numLabel.text = self.tabBarItem.badgeValue;
    }else{
        self.numLabel.hidden = YES;
    }
}


- (void)fastEntryClick:(UIButton *)button
{
    switch (button.tag) {
        case 100:
        {
            RecordViewController *vc = [[RecordViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 101:
        {
            AddViewController *vc = [[AddViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 102:
        {
            CardDetailViewController *vc = [[CardDetailViewController alloc] init];
            vc.circle_Id = @"2";
            vc.circle_name = @"解答区";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 103:
        {
            ChatListViewController *vc = [[ChatListViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}


- (void)creatHotView
{
    _hotView = [[UIView alloc] initWithFrame:CGRectMake(10, 240, ECSCREEN_W - 20, 40)];
    _hotView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    _hotView.clipsToBounds = YES;
    _hotView.layer.cornerRadius = 8;
    [_scrollView addSubview:_hotView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 75, 40)];
    titleLabel.text = @"热门话题";
    titleLabel.textColor = NAVC_COLOR;
    [_hotView addSubview:titleLabel];
    
    UIView *speraLine = [[UIView alloc] initWithFrame:CGRectMake(87.5, 5, 1, 30)];
    speraLine.backgroundColor = ECCOLOR(212, 50, 47, .3f);
    [_hotView addSubview:speraLine];
    
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(95, 0, ECSCREEN_W - 95, 40)];
    detailLabel.text = @"连接论坛，话题自定义滚动";
    detailLabel.font = [UIFont systemFontOfSize:13.f];
    detailLabel.textColor = [UIColor lightGrayColor];
    [_hotView addSubview:detailLabel];
}


- (void)creatHotDetailView
{
    _hotDetailView = [[UIView alloc] initWithFrame:CGRectMake(10, 285, ECSCREEN_W - 20, 150)];
    _hotDetailView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    _hotDetailView.clipsToBounds = YES;
    _hotDetailView.layer.cornerRadius = 8;
    [_scrollView addSubview:_hotDetailView];
    
    
    _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    _headImage.image = ECIMAGENAME(@"header.png");
    _headImage.clipsToBounds = YES;
    _headImage.layer.cornerRadius = 25;
    [_hotDetailView addSubview:_headImage];
    
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 50, 30)];
    _nameLabel.text = @"笑笑";
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont systemFontOfSize:12.f];
    [_hotDetailView addSubview:_nameLabel];
    
    
    _gradeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, ECSCREEN_W - 100, 17)];
    _gradeLabel.text = @"日语等级：N1";
    _gradeLabel.font = [UIFont systemFontOfSize:13.f];
    [_hotDetailView addSubview:_gradeLabel];
    
    
    _schoolLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 37, ECSCREEN_W - 100, 17)];
    _schoolLabel.text = @"毕业院校：BJT商务日语学院";
    _schoolLabel.font = [UIFont systemFontOfSize:13.f];
    [_hotDetailView addSubview:_schoolLabel];
    
    
    _signatureLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 64, ECSCREEN_W - 100, 17)];
    _signatureLabel.text = @"超好笑日语商务日语学院";
    _signatureLabel.font = [UIFont systemFontOfSize:13.f];
    [_hotDetailView addSubview:_signatureLabel];
    
    
    UIView *firstSperaLine = [[UIView alloc] initWithFrame:CGRectMake(0, 100, ECSCREEN_W - 20, 1)];
    firstSperaLine.backgroundColor = ECCOLOR(0, 0, 0, .1f);
    [_hotDetailView addSubview:firstSperaLine];
    
    
    UIImageView *scanImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 107, 35, 35)];
    scanImage.image = ECIMAGENAME(@"scan_num.png");
    [_hotDetailView addSubview:scanImage];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ECSCREEN_W - 20, 100);
    [button addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
    [_hotDetailView addSubview:button];
    
    UILabel *scanLabel;
    if (IS_IPHONE4) {
        scanLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, (ECSCREEN_W - 20)/2 - 50, 50)];
        scanLabel.font = [UIFont systemFontOfSize:10.f];
        _scanNum = [[UILabel alloc] initWithFrame:CGRectMake(51, 0, 27, 50)];
        _scanNum.font = [UIFont systemFontOfSize:10.f];
    }else{
        scanLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, (ECSCREEN_W - 20)/2 - 55, 50)];
        scanLabel.font = [UIFont systemFontOfSize:12.f];
        _scanNum = [[UILabel alloc] initWithFrame:CGRectMake(65, 0, 27, 50)];
        _scanNum.font = [UIFont systemFontOfSize:12.f];
    }
    scanLabel.text = @"主页浏览量(        )";
    [_hotDetailView addSubview:scanLabel];
    

    _scanNum.text = @"";
    _scanNum.textAlignment = NSTextAlignmentCenter;
    _scanNum.textColor = NAVC_COLOR;
    [scanLabel addSubview:_scanNum];
    
    
    UIButton *scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    scanButton.frame = CGRectMake(0, 101, ECSCREEN_W/2 - 10, 49);
    [scanButton addTarget:self action:@selector(scanClick:) forControlEvents:UIControlEventTouchUpInside];
    [_hotDetailView addSubview:scanButton];
    
    
    UIView *secondSperaLine = [[UIView alloc] initWithFrame:CGRectMake((ECSCREEN_W - 20)/2, 110, 1, 30)];
    secondSperaLine.backgroundColor = ECCOLOR(212, 50, 47, .3f);
    [_hotDetailView addSubview:secondSperaLine];
    
    
    UIImageView *rankImage = [[UIImageView alloc] initWithFrame:CGRectMake((ECSCREEN_W - 20)/2 + 10, 107, 35, 35)];
    rankImage.image = ECIMAGENAME(@"rank_num.png");
    [_hotDetailView addSubview:rankImage];
    
    
    UILabel *rankLabel;
    if (IS_IPHONE4) {
        rankLabel = [[UILabel alloc] initWithFrame:CGRectMake((ECSCREEN_W - 20)/2 + 50, 100, (ECSCREEN_W - 20)/2 - 50, 50)];
        rankLabel.font = [UIFont systemFontOfSize:10.f];
        _rankNum = [[UILabel alloc] initWithFrame:CGRectMake(61, 0, 25, 50)];
        _rankNum.font = [UIFont systemFontOfSize:10.f];
    }else{
        rankLabel = [[UILabel alloc] initWithFrame:CGRectMake((ECSCREEN_W - 20)/2 + 50, 100, (ECSCREEN_W - 20)/2 - 55, 50)];
        rankLabel.font = [UIFont systemFontOfSize:12.f];
        _rankNum = [[UILabel alloc] initWithFrame:CGRectMake(75, 0, 25, 50)];
        _rankNum.font = [UIFont systemFontOfSize:12.f];
    }
    rankLabel.text = @"老师综合排名(       )";
    [_hotDetailView addSubview:rankLabel];

    
    _rankNum.text = @"";
    _rankNum.textAlignment = NSTextAlignmentCenter;
    _rankNum.textColor = NAVC_COLOR;
    [rankLabel addSubview:_rankNum];
    
    
    UIButton *rankButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rankButton.frame = CGRectMake(ECSCREEN_W/2 - 10, 101, ECSCREEN_W/2 - 10, 49);
    [rankButton addTarget:self action:@selector(rankClick:) forControlEvents:UIControlEventTouchUpInside];
    [_hotDetailView addSubview:rankButton];

}



- (void)showInfo:(UIButton *)button
{
    InfoViewController *vc = [[InfoViewController alloc] init];
    vc.memberId = _memberModel.member_Id;
    vc.hidesBottomBarWhenPushed = YES;
    
    NSDictionary *param = @{
                            @"teacher_Id":_memberModel.member_Id,
                            @"member_Id":_memberModel.member_Id,
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(CREATSCAN) params:param complete:^(BOOL successed, NSDictionary *result) {
        
    }];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)scanClick:(UIButton *)button
{
    ScanViewController *vc = [[ScanViewController alloc] init];
    vc.model = _scanModel;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)rankClick:(UIButton *)button
{
    RankViewController *vc = [[RankViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)creatTableView
{
    _subscribeView = [UIView new];
    [_scrollView addSubview:_subscribeView];
    
    [_subscribeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_hotDetailView.mas_bottom).offset(5);
        make.width.mas_equalTo(ECSCREEN_W);
        make.height.mas_greaterThanOrEqualTo(40);
    }];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, (ECSCREEN_W - 20)/2, 40)];
    titleLabel.text = @"已约课学生";
    titleLabel.font = [UIFont systemFontOfSize:13.f];
    [_subscribeView addSubview:titleLabel];
    
    
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreButton.frame = CGRectMake(ECSCREEN_W - 10 - 80, 0, 80, 40);
    [moreButton setTitle:@"更多" forState:UIControlStateNormal];
    [moreButton setTitleColor:ECCOLOR(0, 0, 0, 1) forState:UIControlStateNormal];
    moreButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [moreButton setImage:ECIMAGENAME(@"base_more.png") forState:UIControlStateNormal];
    moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, 60, 0, 0);
    [moreButton addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
    [_subscribeView addSubview:moreButton];
    
    
    _tableView = [UITableView new];
    _tableView.backgroundColor = BG_COLOR;
    [_subscribeView addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(0);
        make.left.mas_equalTo(_subscribeView.mas_left).offset(10);
        make.width.mas_equalTo(ECSCREEN_W - 20);
        make.bottom.mas_equalTo(_subscribeView.mas_bottom).offset(5);
    }];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (void)moreClick:(UIButton *)button
{
    NSLog(@"更多");
    MyOrderViewController *vc = [[MyOrderViewController alloc] init];
    vc.index = 1;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 5)];
    view.backgroundColor = BG_COLOR;
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"mainCell";
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"MainCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[MainCell class]]) {
                cell = (MainCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
